//
//  ZCBaseSqliteTool.m
//  VoiceMusic
//
//  Created by dllo on 15/10/23.
//  Copyright © 2015年 zhangdeyu. All rights reserved.
//

#import "ZCBaseSqliteTool.h"

@implementation ZCBaseSqliteTool
#pragma mark 创建单例对象
+(ZCBaseSqliteTool *)shareZCMusicDataTool{
    static ZCBaseSqliteTool *tool;
    static dispatch_once_t onceToKen;
    dispatch_once(&onceToKen, ^{
        tool = [[ZCBaseSqliteTool alloc] init];
        [tool openDB];
        [tool createTable];
    });
    return tool;
}
#pragma mark 打开数据库
-(void)openDB{
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
    NSString *sandBoxPath = sandBox[0];
    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"music.sqlite"];
    NSLog(@"%@",documentPath);
    int result = sqlite3_open([documentPath UTF8String], &dbPoint);
    if (result == SQLITE_OK) {
//        NSLog(@"打开成功");
    }else
    {
//        NSLog(@"打开失败");
    }
}
#pragma mark 创建表格
-(void)createTable
{
    NSString *sqlStr = @"create table if not exists music(number integer primary key autoincrement,name text,singerName text,auditionList text)";
    int result =sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
//        NSLog(@"表创建成功");
    }else{
//        NSLog(@"表创建失败");
    }
}
#pragma mark 添加数据
-(void)insertMusic:(ZCDetailListModel *)music
{
    NSString *sqlStr = [NSString stringWithFormat:@"insert into music (name,singerName,auditionList) values('%@','%@','%@')",music.name,music.singerName,music.auditionList[0][@"url"]];
    int result =sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if(result == SQLITE_OK){
//        NSLog(@"添加成功");
    }else{
//        NSLog(@"添加失败%d",result);
    }
}
#pragma mark 删除数据
-(void)deleteMusic:(ZCDetailListModel *)music
{
    NSString *sqlStr = [NSString stringWithFormat:@"delete from music where name = '%@'",music.name];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
//        NSLog(@"删除成功");
    }else{
//        NSLog(@"删除失败");
    }
    
}
#pragma mark 查询数据
-(NSMutableArray *)selectAllMusicModel
{
    [self openDB];
    [self createTable];
    NSString *sqlStr = @"select * from music";
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    NSMutableArray *musicArr = [NSMutableArray array];
    if (result == SQLITE_OK) {
//        NSLog(@"查询成功");
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            const unsigned char *singerName = sqlite3_column_text(stmt, 2);
            const unsigned char *auditionList = sqlite3_column_text(stmt, 3);
            
            NSString *musicName = [NSString stringWithUTF8String:(const char *)name];
            NSString *musicPhoto = [NSString stringWithUTF8String:(const char *)singerName];
            NSString *musicuId = [NSString stringWithUTF8String:(const char *)auditionList];
            
            ZCDetailListModel *music = [[ZCDetailListModel alloc] init];
            music.name = musicName;
            music.singerName = musicPhoto;
            music.auditionList = (NSMutableArray *)musicuId;
            [musicArr addObject:music];
        }
    }else
    {
//        NSLog(@"查询失败");
    }
    sqlite3_finalize(stmt);
    return musicArr;
}

#pragma mark 判断收藏
-(SelectTable )isInTable:(ZCDetailListModel *)music;
{
    NSString *sqlStr = [NSString stringWithFormat:@"select * from music where name = '%@'",music.name];
    sqlite3_stmt *stmt=nil;
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    if (result == SQLITE_OK) {
//        NSLog(@"查询成功");
        if (sqlite3_step(stmt)== SQLITE_ROW) {
            sqlite3_finalize(stmt);
            return InTable;
        }else{
            sqlite3_finalize(stmt);
            return NotInTable;
        }
    }else{
//        NSLog(@"查询失败");
        sqlite3_finalize(stmt);
        return SelectError;
    }
}

@end
